package logic

import (
	"api/model/order"
	"context"
	"github.com/zeromicro/go-zero/core/stores/sqlx"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderFoodUserOrderListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewOrderFoodUserOrderListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderFoodUserOrderListLogic {
	return &OrderFoodUserOrderListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *OrderFoodUserOrderListLogic) OrderFoodUserOrderList(req *types.RequestOrderFoodUserOrderList) (resp *types.ResponseOrderFoodOrderList, err error) {
	conn := sqlx.NewMysql(l.svcCtx.Config.DataSource)
	orderModel := order.NewOrderModel(conn)

	list, err := orderModel.UserOrderList(l.ctx, req.BusinessId, req.OpenId)
	resp = &types.ResponseOrderFoodOrderList{}
	if list != nil {
		arr := make([]types.OrderFoodOrder, len(*list))
		for index, item := range *list {
			arr[index].InnerTradeNo = item.InnerTradeNo
			arr[index].OrderType = int64(item.Type)
			arr[index].Table = item.Table
			arr[index].Amount = int64(item.Amount)
			arr[index].Discount = int64(item.Discount)
			arr[index].Status = int64(item.Status)
			arr[index].Des = item.InnerTradeNo
			arr[index].CreatAt = item.InnerTradeNo
		}

		resp.List = arr
	}

	resp.Code = 200

	return resp, nil
}
